﻿/*
Copyright (c) 2012 Eric Lasota

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Text;

namespace MPEGnet
{
#if !MPEGNET_FLOAT_AUDIO
    internal struct FixedReal
    {
        int _i;
        public const int BITS = 24;
        public const int OVERFLOW_BITS = 41;

        public int RawValue { get { return _i; } }

        public FixedReal(double f)
        {
            _i = (int)(f * (double)(1 << 24));
        }

        public static FixedReal[] FromDoubles(double[] dbl)
        {
            FixedReal[] fx = new FixedReal[dbl.Length];
            for (int i = 0; i < dbl.Length; i++)
                fx[i] = new FixedReal(dbl[i]);
            return fx;
        }

        public static FixedReal operator -(FixedReal v)
        {
            FixedReal result;
            result._i = -v._i;
            return result;
        }

        public static FixedReal operator -(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._i = ls._i - rs._i;
            return result;
        }

        public static FixedReal operator +(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._i = ls._i + rs._i;
            return result;
        }

        public static FixedReal operator *(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._i = (int)(((long)ls._i * (long)rs._i) >> BITS);
            return result;
        }

        public int MulAndRound(int mul)
        {
            long emul = (long)_i * (long)mul;
            emul += (long)(1 << (BITS - 1));
            return (int)(emul >> BITS);
        }

        // Computes (code * factor) + 1.0 with reduced precision to prevent overflow
        public static FixedReal ShortScale(uint code, FixedReal factor)
        {
            int SHORT_BITS = OVERFLOW_BITS - BITS;
            int codeFixedShort = (int)code << SHORT_BITS;
            long emulFactorShort = ((long)codeFixedShort * (long)factor._i) >> BITS;
            int emulFixedShort = (int)emulFactorShort;
            emulFixedShort -= (1 << SHORT_BITS);

            FixedReal result;
            result._i = emulFixedShort << (BITS - SHORT_BITS);
            return result;
        }
    }
#else
    public struct FixedReal
    {
        private float _f;

        public FixedReal(double f)
        {
            _f = (float)f;
        }

        public static FixedReal[] FromDoubles(double[] dbl)
        {
            FixedReal[] fx = new FixedReal[dbl.Length];
            for (int i = 0; i < dbl.Length; i++)
                fx[i] = new FixedReal(dbl[i]);
            return fx;
        }

        public static FixedReal operator -(FixedReal v)
        {
            FixedReal result;
            result._f = -v._f;
            return result;
        }

        public static FixedReal operator -(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._f = ls._f - rs._f;
            return result;
        }

        public static FixedReal operator +(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._f = ls._f + rs._f;
            return result;
        }

        public static FixedReal operator *(FixedReal ls, FixedReal rs)
        {
            FixedReal result;
            result._f = ls._f * rs._f;
            return result;
        }

        public int MulAndRound(int mul)
        {
            return (int)(_f * (double)mul);
        }

        // Computes (code * factor) + 1.0 with reduced precision to prevent overflow
        public static FixedReal ShortScale(uint code, FixedReal factor)
        {
            FixedReal result;
            result._f = ((float)code * factor._f - 1.0f);
            return result;
        }
    }
#endif
}
